package com.wyk.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyk.project.dao.entity.ShortLinkDO;
import com.wyk.project.dao.mapper.ShortLinkMapper;
import com.wyk.project.dto.req.RecycleBinDeleteReqDTO;
import com.wyk.project.dto.req.RecycleBinRecoverReqDTO;
import com.wyk.project.dto.req.RecycleBinSaveReqDTO;
import com.wyk.project.dto.req.ShortLinkRecycleBinPageReqDTO;
import com.wyk.project.dto.resp.ShortLinkPageRespDTO;
import com.wyk.project.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.wyk.project.common.constant.RedisKeyConstant.GOTO_IS_NOLL_SHORT_LINK_KEY;
import static com.wyk.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;

@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper,ShortLinkDO > implements RecycleBinService  {

    private final StringRedisTemplate stringRedisTemplate;
    /**
     * 保存回收站
     * @param requestParam
     */
    @Override
    public void saveRecycle(RecycleBinSaveReqDTO requestParam) {
        ShortLinkDO build = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(build, Wrappers.lambdaUpdate(ShortLinkDO.class)
                        .eq(ShortLinkDO::getGid, requestParam.getGid())
                        .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                        .eq(ShortLinkDO::getEnableStatus,0)
                        .eq(ShortLinkDO::getDelFlag,0));
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY,requestParam.getFullShortUrl()));
    }

    /**
     * 回收站分页
     * @param requestParam
     * @return
     */
    @Override
    public IPage<ShortLinkPageRespDTO> pageshortLink(ShortLinkRecycleBinPageReqDTO requestParam){
        IPage<ShortLinkDO> resultPage = baseMapper.pageRecycleBinLink(requestParam);
        return resultPage.convert(each -> {
            ShortLinkPageRespDTO shortLinkPageRespDTO = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            shortLinkPageRespDTO.setDomain("http://" + shortLinkPageRespDTO.getDomain());
            return  shortLinkPageRespDTO;
        });
    }

    /**
     * 回收站恢复短连接
     * @param requestParam
     */
    @Override
    public void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam) {
        ShortLinkDO build = ShortLinkDO.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(build,Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getEnableStatus,1)
                .eq(ShortLinkDO::getDelFlag,0)
                .eq(ShortLinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid, requestParam.getGid()));
        stringRedisTemplate.delete(String.format(GOTO_IS_NOLL_SHORT_LINK_KEY,requestParam.getFullShortUrl()));
    }

    /**
     * 移除回收站短连接
     * @param requestParam
     */
    @Override
    public void removeRecycleBin(RecycleBinDeleteReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> deleteWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag,0)
                .eq(ShortLinkDO::getDelTime,0L)
                .eq(ShortLinkDO::getEnableStatus, 1);
        ShortLinkDO delShortLinkDO = ShortLinkDO.builder()
                .delTime(System.currentTimeMillis())
                .build();
        delShortLinkDO.setDelFlag(1);
        baseMapper.update(delShortLinkDO,deleteWrapper);
    }
}
